﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Nuane.Posterous;
using System.Threading;

namespace Writerous
{
    internal partial class frmPublishing : Form
    {
        internal PosterousPostInfo m_publishedPost;
        private PosterousNewPost m_newPost;
        private PosterousSession m_session;
        private int m_siteID;
        internal bool isPublishing { get { return m_publishThePost.IsBusy; } }
                
        internal frmPublishing(PosterousNewPost newPost, PosterousSession session, int siteID)
        {
            InitializeComponent();
            m_publishThePost = new BackgroundWorker();
            m_publishThePost.DoWork += new DoWorkEventHandler(m_publishThePost_DoWork);
            m_publishThePost.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_publishThePost_RunWorkerCompleted);
            m_newPost = newPost;
            m_siteID = siteID;
            m_session = session;
        }

        internal void GoBatman()
        {
            object[] args = { m_newPost, m_session, m_siteID };
            m_publishThePost.RunWorkerAsync(args);
        }

        void m_publishThePost_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            m_publishedPost = e.Result as PosterousPostInfo;
            this.Close();
        }

        void m_publishThePost_DoWork(object sender, DoWorkEventArgs e)
        {
            object[] args = e.Argument as object[];
            PosterousNewPost post = args[0] as PosterousNewPost;
            PosterousSession session = args[1] as PosterousSession;
            int siteID = Convert.ToInt32(args[2]);
            e.Result = session.PublishPost(siteID, post);
        }

        
    }    
}
